<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

use think\captcha\Captcha;
use app\lib\aliyun\Api as aliyunApi;


class Zgh extends Controller
{
    public function top(){
        set_time_limit ( 0 );
        ini_set('memory_limit','10240M');

        if (request()->isPost()) {
            $fields = [
                'must'=>['accessKeyId','accessKeySecret'],
            ];
            $params = paramFilter(request()->param(),$fields);
            if (!empty($params['error_msg'])) {
                $this->error($params['error_msg']);
            }
            // $params['tags'] = explode(',',$params['tags']);
            $domains = explode(',',$params['domains']);
            $domains = ['meet.cgnuk.co.uk','dts.cgnuk.co.uk','appstore.cgnpc.com.cn','imsqs-ar.cgnpc.com.cn','zdm.cgnpc.com.cn','ibc.cgnpc.com.cn','www.cgnpe.com.cn','imsqs.cgnpc.com.cn','svms.cgnpc.com.cn','pbs-srv.cgnpc.com.cn','idms.cgnpc.com.cn','cgnfms-lp.cgnpc.com.cn','cgnfms.cgnpc.com.cn','cgnpe-fms.cgnpc.com.cn','ensure.cgnpc.com.cn','sp.cgnscfs.com.cn','agw.cgnscfs.com.cn','cgn-csonline.cgnpc.com.cn','trip.cgnpc.com.cn','ecp.cgnpc.com.cn','fes.cgnpc.com.cn','dts.cgnpc.com.cn','learning-dnmc.cgnpc.com.cn','learn-dnmc.cgnpc.com.cn','learn-cbs.cgnpc.com.cn','learn.cgnpc.com.cn','4a.cgnpc.com.cn','uat.ecp.cgnpc.com.cn','uice.cgnpc.com.cn','uic.cgnpc.com.cn','landing.cgnpc.com.cn','newmobileapp.cgnpc.com.cn','mailbox.cgnpc.com.cn','mapp.cgnpc.com.cn','uap-dt.cgnpc.com.cn','uap-mt.cgnpc.com.cn','mcs.cgnpc.com.cn','uap.cgnpc.com.cn','dingnewsapp.cgnpc.com.cn','dingaueapp.cgnpc.com.cn','party.cgnpc.com.cn','jt-mail.cgnpc.com.cn','cgnservice.cgnpc.com.cn','hrssc.cgnpc.com.cn','nmr.cgnpc.com.cn','uem.cgnpc.com.cn','entry.cgnpc.com.cn','mall.cgnpc.com.cn','job.cgnpc.com.cn','dts.cgnpc.biz','apes.cgncit.com.cn','www.cgn-healthcare.com','cnpscdr.cgnpc.com.cn','stems.cgnpc.com.cn','ndlbs-ndnp.cgnpc.com.cn','osst.cgnpc.com.cn','nscm-cnpec.cgnpc.com.cn','imshs.cgnpc.com.cn','imscqp.cgncit.com.cn','imsca.cgncit.com.cn','nicean.cgncit.com.cn','lfics.cgnpc.com.cn','pimos.cgnpc.com.cn','nicefile.cgnpc.com.cn','nicelf.cgnpc.com.cn','nicebj.cgnpc.com.cn','nicebs.cgnpc.com.cn','nicean.cgnpc.com.cn','srv.cgnpc.com.cn','imsce.cgnpc.com.cn','imsesvapp.cgnpc.com.cn','dingnepapp.cgnpc.com.cn','ciqcs.cgnpc.com.cn','ctmtp.cgnpc.com.cn','nice03.cgnpc.com.cn','imssu.cgnpc.com.cn','imsca.cgnpc.com.cn','nice.cgnpc.com.cn','cgnss.cgnpc.com.cn','imsime.cgnpc.com.cn','jlzf.cgnpc.com.cn','imstax.cgnpc.com.cn','cems.cgnpc.com.cn','anan.cgnpc.com.cn','microflow.cgnpc.com.cn','npiot.cgnpc.com.cn','testu.jtmet.com','api.hhyungu.com','mobile.hhyungu.com','resource.hhyungu.com','seller.hhyungu.com','www.hhyungu.com','www.hdgwtwo.cn','www.wxnzh.cn','www.wxnzh.com','nesea.iowfsop.com','wap.iowfsop.com','uat.iowfsop.com','www.iowfsop.com','tsfods.cgnpc.com.cn','tsly.cgnpc.com.cn','hyhly2.cgnpc.com.cn','hyhly.cgnpc.com.cn','hbzhsw.cgnpc.com.cn','online.cgnpc.com.cn','hfrms.cgnpc.com.cn','ehs.cgnpc.com.cn','zhncz.cgnpc.com.cn','sqms.cgnpc.com.cn','cgnswy-pic.cgnpc.com.cn','cgnswy.cgnpc.com.cn','ecard.cgnpc.com.cn','cgnnecms.cgncit.com.cn','mpms.cgnpc.com.cn','fenbushiyunpingtai.com','nepm.cgnpc.com.cn','cgntc.cgnpc.com.cn','nesea.cgnpc.com.cn','nespo.cgnpc.com.cn','neom.cgnpc.com.cn','parts.cgnpc.com.cn','cgnservicetest.cgnpc.com.cn','mds-testing.cgnpc.com.cn','sqms-t.cgnpc.com.cn','www.cnpec.com.cn','en.cgnurc.com.cn','www.cgnurc.com.cn','www.fcgnp.com.cn','www.yjnp.com.cn','en.cnpec.com.cn','sp-t.cgnscfs.com.cn','agw-t.cgnscfs.com.cn','www.cgnhb.com','www.nnppsr.org.cn','www.en.cgndasheng.com','www.cgndasheng.com','www.swakopuranium.com','www.lhnp.com.cn','www.tpvco.cgnam.cn','www.zynpc.com.cn','www.cgnjy.com','www.en.cgnam.cn','www.cgnam.cn','www.bfxclsz.cgnjuner.com','www.toxxon.cgnjuner.com','www.en.cgnjuner.com','www.cgnjuner.com','www.cgnyptp.com.cn','www.cgnei.com','cnpsc.cnpri.com.cn','www.cgns.com.cn','www.cgnnt.com.cn','www.cgncl.com.cn','www.cgncapital.com.cn','','','','www.cgnne.com','','','','www.cgnmc.com','','','','www.cgcnp.com.cn','','','','www.ctecdcs.com','','','','www.cnoc.com.cn','','','','www.hznpc.com.cn','','','','www.lfnp.com.cn','','','','en.tnpjvc.com.cn','','','','www.tnpjvc.com.cn','','','','www.ndnp.com.cn','','','','www.snpi.ac.cn','','','','www.cnpri.com.cn','','','','en.dnmc.com.cn','','','','www.dnmc.com.cn','','','','en.cgnp.com.cn','','','','www.cgnp.com.cn','','','','en.cgnpc.com.cn','','','','www.cgnpc.com.cn','','','','newmobileapptest.cgnpc.com.cn','','trip-p.cgnpc.com.cn','','imsqs-t.cgnpc.com.cn','','dtssyncservice-t.cgnpc.com.cn','','hrwxtest.cgnpc.com.cn','hrwxdoctest.cgnpc.com.cn','nmr-wxtest.cgnpc.com.cn','dingnepapptest.cgnpc.com.cn','ossttest.cgnpc.com.cn','partstest.cgnpc.com.cn','mobileapptest.cgnpc.com.cn','ensure-t.cgnpc.com.cn','','fes-p.cgnpc.com.cn','','imsqs-ar-t.cgnpc.com.cn','','mcs-t.cgnpc.com.cn','','trip-t.cgnpc.com.cn','','uap-mt-t.cgnpc.com.cn','','uap-t.cgnpc.com.cn','','stems-urc-t.cgnpc.com.cn','pimos-t.cgnpc.com.cn','newapp-t.cgnpc.com.cn','nespo-t.cgnpc.com.cn','nesea-t.cgnpc.com.cn','neom-t.cgnpc.com.cn','mall-p.cgnpc.com.cn','learning-test.cgnpc.com.cn','hdt-t.cgnpc.com.cn','entry-t.cgnpc.com.cn','ensure-p.cgnpc.com.cn','elecsign-t.cgnpc.com.cn','ehs-t.cgnpc.com.cn','ctmtp-t.cgnpc.com.cn','cgnss-t.cgnpc.com.cn','cgnpe-fms-t.cgnpc.com.cn','cems-t.cgnpc.com.cn','4a-t.cgnpc.com.cn','','nice03.cgncit.com.cn','nice.cgncit.com.cn','nicefile.cgncit.com.cn','nicecw.cgncit.com.cn','nicebs.cgncit.com.cn','nicelf.cgncit.com.cn','nicebj.cgncit.com.cn','www.cgncit.com.cn','','test-appstore.cgnpc.com.cn'];

            $api_controller = new aliyunApi();
            $exports = [];
            foreach ($domains as $k => $domain) {
                if (empty($domain)) {
                    continue;
                }
                // $result = $api_controller->DescribeDcdnDomainOriginTrafficData($params['accessKeyId'],$params['accessKeySecret'],$domain);
                $result = $api_controller->DescribeDcdnDomainRealTimeSrcBpsData($params['accessKeyId'],$params['accessKeySecret'],$domain);
                // dump($result);die;
                if ($result['status'] == 1) {
                    $list = $result['data']['RealTimeSrcTrafficDataPerInterval']['DataModule'];
                    // dump($list);die;
                    // $data = [
                    //     'domainName'=>$domain,
                    //     'DynamicHttpOriginTraffic'=>array_sum(array_column($list, 'DynamicHttpOriginTraffic')),
                    //     'DynamicHttpsOriginTraffic'=>array_sum(array_column($list, 'DynamicHttpsOriginTraffic')),
                    //     'OriginTraffic'=>array_sum(array_column($list, 'OriginTraffic')),
                    //     'StaticHttpOriginTraffic'=>array_sum(array_column($list, 'StaticHttpOriginTraffic')),
                    //     'StaticHttpsOriginTraffic'=>array_sum(array_column($list, 'StaticHttpsOriginTraffic')),
                    // ];
                    $data = [
                        $domain,
                        array_sum(array_column($list, 'Value')),
                    ];
                    // dump($data);die;
                    $exports[] = $data;
                }
            }


            // $title = ['域名','动态HTTP回源流量数据','动态HTTPS回源流量数据','回源流量数据','静态HTTP回源流量数据','静态HTTPS回源流量数据'];
            $title = ['域名','回源带宽数据'];
            
            exportCsv(date('YmdHis'),$title,$exports);
            exit();     
        }
        return view('top', [
            
        ]);
    }

    public function export(){
        set_time_limit ( 0 );
        ini_set('memory_limit','10240M');

        if (request()->isPost()) {
            $fields = [
                'must'=>['accessKeyId','accessKeySecret','tags'],
            ];
            $params = paramFilter(request()->param(),$fields);
            if (!empty($params['error_msg'])) {
                $this->error($params['error_msg']);
            }
            $params['tags'] = explode(',',$params['tags']);

            $api_controller = new aliyunApi();
            $result = $api_controller->DescribeDcdnUserDomains($params['accessKeyId'],$params['accessKeySecret']);
            if ($result['status'] == 0) {
                $this->error($result['data']['message']);
            }
            $list = $result['data']['Domains']['PageData'];
            foreach ($list as $k => $vo) {
                // $tags = ['tag1'=>'','tag2'=>'','tag3'=>'','tag4'=>'','tag5'=>'','tag6'=>'','tag7'=>'','tag8'=>''];
                $tags = [];
                $result = $api_controller->DescribeDcdnTagResources($params['accessKeyId'],$params['accessKeySecret'],$vo['DomainName']);
                if ($result['status'] == 1) {
                    if (!empty($result['data']['TagResources'])) {
                        foreach ($result['data']['TagResources'] as $tk => $tv) {
                            foreach ($tv['Tag'] as $tk2 => $tv2) {
                                // $tags['tag'.($tk2+1)] = $tv2['Key'].':'.$tv2['Value'];
                                $tags[$tv2['Key']] = $tv2['Value'];
                            }
                        }
                    }
                }else{
                    $tags[$params['params'][0]] = $result['message'];
                }
                $list[$k]['tags'] = $tags;

                $result = $api_controller->DescribeDcdnDomainCname($params['accessKeyId'],$params['accessKeySecret'],$vo['DomainName']);
                if ($result['status'] == 0 || !isset($result['data']['CnameDatas']['Data'][0]['Status'])) {
                    $list[$k]['cname_status'] = 0;
                }elseif($result['data']['CnameDatas']['Data'][0]['Status'] > 0){
                    $list[$k]['cname_status'] = -1;
                }else{
                    $list[$k]['cname_status'] = 1;
                }

                $result = $api_controller->DescribeDcdnDomainDetail($params['accessKeyId'],$params['accessKeySecret'],$vo['DomainName']);
                $list[$k]['scope'] = $result['data']['DomainDetail']['Scope'];
            }

            $title = ['CNAME','加速域名','运行状态','创建时间','是否在沙箱','HTTPS状态','Source','CNAME状态','加速区域'];
            $title = array_merge($title,(array)$params['tags']);
            

            $data = [];

            $status = ['online'=>'启用','offline'=>'停用','configuring'=>'配置中','configure_failed'=>'配置失败','checking'=>'正在审核','check_failed'=>'审核失败'];
            foreach ($list as $k => $vo) {
                $tmp = [
                    $vo['Cname'],$vo['DomainName'],$status[$vo['DomainStatus']],
                    date('Y-m-d H:i:s',strtotime($vo['GmtCreated'])),
                    $vo['Sandbox'],
                    $vo['SSLProtocol'] == 'on' ? '已开启' : '未开启',
                    str_replace(',', '|', json_encode($vo['Sources']['Source'],JSON_UNESCAPED_UNICODE)),
                    $vo['cname_status'],$vo['scope']
                ];
                foreach ($params['tags'] as $k2 => $v2) {
                    $tmp[] = $vo['tags'][$v2];
                }
                $data[] = $tmp;
            }
            exportCsv(date('YmdHis'),$title,$data);
            exit();     
        }
        return view('export', [
            
        ]);
    }

    public function verify(){
        set_time_limit ( 0 );
        ini_set('memory_limit','10240M');

        if (request()->isPost()) {
            $fields = [
                'must'=>['accessKeyId','accessKeySecret'],
            ];
            $params = paramFilter(request()->param(),$fields);
            if (!empty($params['error_msg'])) {
                $this->error($params['error_msg']);
            }

            $file = $_FILES['file'];
            if (empty($file)) {
                $this->error('未检测到文件');
            }

            $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file['tmp_name']);
            $list=$spreadsheet->getsheet(0)->toArray();   //转换为数组格式
            array_shift($list);  //删除第一个数组(标题);

            $api_controller = new aliyunApi();
            $results = [];
            foreach ($list as $k => $vo) {
                $domainName = $vo[0];
                if (empty($domainName)) {
                    continue;
                }
                $result = $api_controller->DescribeDcdnVerifyContent($params['accessKeyId'],$params['accessKeySecret'],$domainName);
                $result2 = [
                    'domainName'=>$domainName,
                    'data'=>$result
                ];
                Db::name('test')->insert(['content'=>json_encode($result2,JSON_UNESCAPED_UNICODE)]);
                $results[] = $result2;
            }
            $this->assign([
                'results'=>$results
            ]);            
        }
        return view('verify', [
            
        ]);
    }


    public function import(){
        set_time_limit ( 0 );
        ini_set('memory_limit','10240M');

        if (request()->isPost()) {
            $fields = [
                'must'=>['accessKeyId','accessKeySecret'],
            ];
            $params = paramFilter(request()->param(),$fields);
            if (!empty($params['error_msg'])) {
                $this->error($params['error_msg']);
            }

            $file = $_FILES['file'];
            if (empty($file)) {
                $this->error('未检测到文件');
            }

            $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file['tmp_name']);
            $list=$spreadsheet->getsheet(0)->toArray();   //转换为数组格式
            array_shift($list);  //删除第一个数组(标题);

            $api_controller = new aliyunApi();
            $results = [];
            foreach ($list as $k => $vo) {
                $domainName = $vo[0];
                if (empty($domainName)) {
                    continue;
                }
                $ip_addr = $vo[2];
                if (empty($ip_addr)) {
                    $sources = $vo[1];
                }else{
                    $type = $vo[4];
                    if (empty($type)) {
                        $type = 'ipaddr';
                    }
                    $sources = [
                        'type'=>$type,
                        'content'=>$ip_addr,
                        'port'=>$vo[3],
                    ];
                    if (!empty($vo[5])) {
                        $sources['priority'] = $vo[5];
                    }
                    if (!empty($vo[6])) {
                        $sources['weight'] = $vo[6];
                    }
                    $sources = [$sources];
                    $sources = json_encode($sources,JSON_UNESCAPED_UNICODE);
                }
                $scope = $vo[7];
                if (empty($scope)) {
                    $scope = 'domestic';
                }
                $result = $api_controller->AddDcdnDomain($params['accessKeyId'],$params['accessKeySecret'],$domainName,$sources,$scope);
                $result2 = [
                    'domainName'=>$domainName,
                    'data'=>$result
                ];
                Db::name('test')->insert(['content'=>json_encode($result2,JSON_UNESCAPED_UNICODE)]);
                $results[] = $result2;
            }

            $this->assign([
                'results'=>$results
            ]);            
        }
        return view('import', [
            
        ]);
    }

}
